package com.etoak.controller;


import com.etoak.common.core.vo.PageVO;
import com.etoak.common.core.vo.ResultVO;
import com.etoak.entity.Dict;
import com.etoak.entity.Ingredients;
import com.etoak.service.DictService;
import com.etoak.service.IngredientsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author et2403
 * @since 2024-08-13
 */
@RestController
@RequestMapping("/ingredients")
public class IngredientsController {

    @Autowired
    IngredientsService ingredientsService;

    /**
     * 添加食材
     */
    @PostMapping("/add")
    public ResultVO<String> addIngredients(@Validated @RequestBody Ingredients ingredients) {
        boolean flag = ingredientsService.addIngredients(ingredients);
        if (flag) {
            return ResultVO.success();
        } else {
            return ResultVO.failed();
        }
    }

    /**
     * 食材列表
     */
    @GetMapping
    public ResultVO<PageVO> getList(@RequestParam(required = false, defaultValue = "1") int pageNum,
                                    @RequestParam(required = false, defaultValue = "10") int pageSize,
                                    Ingredients ingredients) {
        PageVO vo = ingredientsService.getList(pageNum, pageSize, ingredients);
        return ResultVO.success(vo);
    }

    /**
     * 更新 put /ingredients/{id}
     */
    @PutMapping("/{id}")
    public ResultVO<String> updateIngredients(@PathVariable int id, @RequestBody Ingredients ingredients) {
        boolean flag = ingredientsService.updateIngredients(id, ingredients);
        if (flag) {
            return ResultVO.success();
        } else {
            return ResultVO.failed();
        }
    }

    /**
     * 删除食材 delete /ingredient/{id}
     */
    @DeleteMapping("/{id}")
    public ResultVO deleteIngredients(@PathVariable int id) {
        boolean flag = ingredientsService.deleteById(id);
        if (flag) {
            return ResultVO.success();
        } else {
            return ResultVO.failed();
        }
    }

}

